package com.example.smart.controller;

import com.example.common.util.R;
import com.example.item.Navmenu;
import com.example.smart.service.NavMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/navMenu/")
@Tag(name = "显示导航菜单数据")
@Slf4j
public class NavMenuController {
    @Autowired
    private NavMenuService navMenuService;

    @PreAuthorize("hasAuthority('navMenu:list')")
    @Operation(summary = "获得所有的导航菜单数据")
    @GetMapping(value = "/getAllNavMenu")
    public R getAllNavMenu() {
        List<Navmenu> navMenuList = navMenuService.list();
        return R.ok("查询所有导航菜单数据成功").data("navMenuList", navMenuList);
    }

    @PreAuthorize("hasAuthority('navMenu:add')")
    @Operation(summary = "增加导航菜单")
    @PostMapping(value = "/addNavMenu")
    public R addNavMenu(@RequestBody Navmenu navMenu) {
        boolean flag = navMenuService.save(navMenu);
        if (flag) {
            return R.ok("增加导航菜单成功");
        } else {
            return R.error("增加导航菜单失败");
        }
    }

    @PreAuthorize("hasAuthority('navMenu:delete')")
    @Operation(summary = "删除导航菜单数据")
    @DeleteMapping(value = "/deleteNavMenu/{id}")
    public R deleteNavMenu(@PathVariable("id") Integer navMenuId) {
        boolean flag =  navMenuService.removeById(navMenuId);
        if (flag) {
            return R.ok("删除导航菜单成功");
        } else {
            return R.error("删除导航菜单失败");
        }
    }

    @PreAuthorize("hasAuthority('navMenu:getById')")
    @Operation(summary = "按照导航菜单编号查询数据")
    @GetMapping(value = "/getNavMenuById/{id}")
    public R getByIdNavMenu(@PathVariable("id") Integer navMenuId) {
        Navmenu navMenu = navMenuService.getById(navMenuId);
        return R.ok("按照导航菜单编号查询数据成功").data("navMenu", navMenu);
    }

    @PreAuthorize("hasAuthority('navMenu:update')")
    @Operation(summary = "修改导航菜单数据")
    @PutMapping(value = "/updateNavMenu")
    public R updateNavMenu(@RequestBody Navmenu navMenu) {
        boolean flag =  navMenuService.updateById(navMenu);
        if (flag) {
            return R.ok("修改导航菜单成功");
        } else {
            return R.error("修改导航菜单失败");
        }
    }
}
